package one.stand.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jmp.base.CompanyApi;
import com.jmp.base.dto.*;
import one.stand.facade.CompanyCardFacadeService;
import one.stand.mapper.CompanyContentMapperExt;
import one.stand.model.*;
import one.stand.service.CompanyLicenseService;
import one.stand.service.CompanyService;
import one.stand.service.CompanyServiceService;
import one.stand.service.WebsiteService;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author cjr
 */
@RestController
public class CompanyController implements CompanyApi {

    @Autowired
    private WebsiteService websiteService;

    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyServiceService companyServiceService;
    @Autowired
    private CompanyCardFacadeService companyCardFacadeService;
    @Autowired
    private CompanyLicenseService companyLicenseService;
    @Autowired
    private CompanyContentMapperExt companyContentMapperExt;

    @Override
    public ResultModel<CompanyAuditInfoResponse> auditInfo(@RequestBody CompanyAuditInfoRequest request) {
        CompanyLicense companyLicense = companyLicenseService.getByCompanyId(request.getCompanyId());
        CompanyAuditInfoResponse response = CompanyAuditInfoResponse.builder()
                .status(Objects.nonNull(companyLicense) ? companyLicense.getStatus() : 0)
                .build();
        return ResultModel.success(response);
    }

    @Override
    public ResultModel levelUpdate(@RequestBody CompanyLevelUpdateRequest request) {
        companyService.updateLevel(request.getCompanyId(), request.getLevel());
        return ResultModel.success();
    }

    @Override
    public ResultModel edit(@RequestBody CompanyEditRequest request) {
        Company outModel = new Company();
        outModel.setCompanyId(request.getCompanyId());
        outModel.setCompanyLogo(request.getCompanyLogo());
        outModel.setCompanyName(request.getCompanyName());
        outModel.setPassword(request.getPassword());
        outModel.setIndustryId(request.getIndustryId());
        outModel.setCompanyNotice(request.getCompanyNotice());
        outModel.setPhone(request.getPhone());
        outModel.setAddrArea(request.getAddrArea());
        outModel.setAddrStreet(request.getAddrStreet());
        outModel.setAddrNo(request.getAddrNo());
        companyService.update(outModel);

        if (StringUtils.isNoneBlank(request.getAddress())) {
            Website website = websiteService.getByCompanyId(request.getCompanyId());
            Integer websiteId;
            if (Objects.nonNull(website)) {
                websiteId = website.getWebsiteId();
            } else {
                websiteId = websiteService.saveEmpty(request.getCompanyId());
            }
            Website websiteUpdate = new Website();
            websiteUpdate.setWebsiteId(websiteId);
            websiteUpdate.setAddress(request.getAddress());
            websiteUpdate.setLatitude(request.getLatitude());
            websiteUpdate.setLongitude(request.getLongitude());
            websiteService.update(websiteUpdate);
        }

        if (StringUtils.isNoneBlank(request.getLicensePhotos())) {
            companyLicenseService.add(request.getCompanyId(), request.getLicensePhotos(), null);
        }

        return ResultModel.success(true);
    }

    @Override
    public ResultModel quit(@RequestBody CompanyQuitRequest request) {
        companyCardFacadeService.quitCompany(request.getCardId(), request.getMyCardId());
        return ResultModel.success();
    }

    @Override
    public ResultModel transfer(@RequestBody CompanyTransferRequest request) {

        companyCardFacadeService.transfer(request.getUserId(), request.getCompanyId());

        return ResultModel.success();
    }

    @Override
    public ResultModel dissolution(@RequestBody CompanyDissolutionRequest request) {

        companyCardFacadeService.dissolution(request.getCompanyId());

        return ResultModel.success();
    }

    @Override
    public ResultModel cardUpdate(@RequestBody CompanyCardUpdateRequest request) {
        companyService.updateCompanyCard(request.getCompanyId(), request.getCardId());
        return ResultModel.success();
    }

    @Override
    public ResultModel<CompanyCardResponse> card(@RequestBody CompanyCardRequest request) {
        Integer cardId = companyService.getCompanyCard(request.getCompanyId());
        CompanyCardResponse response = CompanyCardResponse.builder().cardId(cardId).build();
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<CompanyCardResponse> cardId(@RequestBody CompanyCardIdRequest request) {
        Integer cardId = companyService.getCompanyCardById(request.getId());
        CompanyCardResponse response = CompanyCardResponse.builder().cardId(cardId).build();
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<CompanySuncodeResponse> suncode(@RequestBody CompanySuncodeRequest request) {
        String codeUrl = companyService.getSuncode(request.getCompanyId());
        CompanySuncodeResponse response = CompanySuncodeResponse.builder()
                .codeUrl(codeUrl)
                .build();
        return ResultModel.success(response);
    }

    @Override
    public ResultModel contentAdd(@RequestBody CompanyContentAddRequest request) {
        CompanyContent companyContent = companyContentMapperExt.selectByCompanyId(request.getCompanyId());
        if (Objects.isNull(companyContent)) {
            companyContent = new CompanyContent();
            companyContent.setCompanyId(request.getCompanyId());
            companyContent.setPushNum(0);
            companyContentMapperExt.insertSelective(companyContent);
        } else {
            CompanyContent companyContentUpdate = new CompanyContent();
            companyContentUpdate.setId(companyContent.getId());
            companyContentUpdate.setIsDel(BooleanUtils.toInteger(false));
            companyContentMapperExt.updateByPrimaryKeySelective(companyContentUpdate);
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel contentRemove(@RequestBody CompanyContentAddRequest request) {
        CompanyContent companyContent = companyContentMapperExt.selectByCompanyId(request.getCompanyId());
        if (Objects.isNull(companyContent)) return ResultModel.success();

        CompanyContent companyContentUpdate = new CompanyContent();
        companyContentUpdate.setId(companyContent.getId());
        companyContentUpdate.setIsDel(BooleanUtils.toInteger(true));
        companyContentMapperExt.updateByPrimaryKeySelective(companyContentUpdate);
        return ResultModel.success();
    }

    @Override
    public ResultModel contentPush(@RequestBody CompanyContentAddRequest request) {
        CompanyContent companyContent = companyContentMapperExt.selectByCompanyId(request.getCompanyId());
        CompanyContent companyContentUpdate = new CompanyContent();
        companyContentUpdate.setId(companyContent.getId());
        companyContentUpdate.setPushNum(companyContent.getPushNum() + 1);
        companyContentMapperExt.updateByPrimaryKeySelective(companyContentUpdate);
        return ResultModel.success();
    }

    @Override
    public ResultModel serviceAdd(@RequestBody CompanyServiceAddRequest request) {
        companyServiceService.add(request.getCompanyId(), request.getPurchaseNum(), request.getEndTime(), request.getPayNum(), request.getUserId());
        return ResultModel.success();
    }

    @Override
    public ResultModel licenseUpdate(@RequestBody CompanyLicenseUpdateRequest request) {
        if(Objects.nonNull(request.getCompanyId())) {
            companyLicenseService.add(request.getCompanyId(), CollUtil.join(request.getLicensePhotos(), StrUtil.COMMA), null);
        } else {
            companyLicenseService.update(request.getId(), request.getType(), request.getLicensePhotos());
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel licenseAudit(@RequestBody CompanyLicenseAuditRequest request) {
        if (request.getAudit() == 1) {
            companyCardFacadeService.companyAudit(request.getId());
        } else {
            companyCardFacadeService.companyRefuse(request.getId(), request.getMemo());
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<CompanyLicenseInfoResponse> licenseInfo(@RequestBody CompanyLicenseInfoRequest request) {
        CompanyLicense companyLicense = companyLicenseService.getByCompanyId(request.getCompanyId());

        CompanyLicenseInfoResponse.CompanyLicenseInfoResponseBuilder response = CompanyLicenseInfoResponse.builder();
        if (Objects.nonNull(companyLicense)) {
            response.id(companyLicense.getId())
                    .companyId(companyLicense.getCompanyId())
                    .status(companyLicense.getStatus())
                    .refuseReason(companyLicense.getMemo())
                    .photos(companyLicense.getPhotos());
        }
        return ResultModel.success(response.build());
    }
}
